﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccessLayer
{
    public class PurchaseOrderRepository
    {
        StationeryInventoryEntities1 ent;

        public PurchaseOrderRepository()
        {
            ent = new StationeryInventoryEntities1();
        }

        #region Select Method

        public List<PurchaseOrder> GetPurchaseOrderByStatus(string status)
        {
            List<PurchaseOrder> list = new List<PurchaseOrder>();

            var po = ent.PurchaseOrders.Where(x => x.Status == status);

            if (po != null)
            {
                list = po.ToList();
            }
            return list;
        }

        public List<PurchaseOrder> GetPurchaseOrderBySupplierIdAndStatus(string supplierId, string status)
        {
            List<PurchaseOrder> list = new List<PurchaseOrder>();

            var po = ent.PurchaseOrders.Where(x => x.SupplierID == supplierId && x.Status == status);

            if (po != null)
            {
                list = po.ToList();
            }
            return list;
        }

        public List<PurchaseOrder> GetPurchaseOrderByRequestedByAndStatus(int requestedBy, string status)
        {
            List<PurchaseOrder> list = new List<PurchaseOrder>();

            var po = ent.PurchaseOrders.Where(x => x.RequestedBy == requestedBy && x.Status == status);

            if (po != null)
            {
                list = po.ToList();
            }
            return list;
        }

        public List<PurchaseOrder> GetPurchaseOrderBySupplierIdAndRequestedByAndStatus(string supplierId, int requestedBy, string status)
        {
            List<PurchaseOrder> list = new List<PurchaseOrder>();

            var po = ent.PurchaseOrders.Where(x => x.SupplierID == supplierId && x.RequestedBy == requestedBy && x.Status == status);

            if (po != null)
            {
                list = po.ToList();
            }
            return list;
        }

        public PurchaseOrder GetPurchaseOrderByPOId(int POId)
        {
            PurchaseOrder purchaseOrder = new PurchaseOrder();

            var po = ent.PurchaseOrders.FirstOrDefault(x => x.POID == POId);

            if (po != null)
            {
                purchaseOrder = po;
            }
            return purchaseOrder;
        }
        #endregion

        #region Create Method
        public void CreatePurchaseOrder(PurchaseOrder purchaseOrder)
        {
            ent.PurchaseOrders.Add(purchaseOrder);
            ent.SaveChanges();
        }
        #endregion

        #region Update Method
        public void UpdatePurchaseOrder(PurchaseOrder purchaseOrder)
        {
            var po = ent.PurchaseOrders.FirstOrDefault(x => x.POID == purchaseOrder.POID);

            po.RequestedDate = purchaseOrder.RequestedDate;
            po.RequestedBy = purchaseOrder.RequestedBy;
            po.ExpectedDeliveryDate = purchaseOrder.ExpectedDeliveryDate;
            po.SupplierID = purchaseOrder.SupplierID;
            po.Attn = purchaseOrder.Attn;
            po.ApprovalDate = purchaseOrder.ApprovalDate;
            po.ApprovalBy = purchaseOrder.ApprovalBy;
            po.Status = purchaseOrder.Status;
            po.DeliveryDate = purchaseOrder.DeliveryDate;
            po.DeliveryOrderNo = purchaseOrder.DeliveryOrderNo;
            po.Reason = purchaseOrder.Reason;

            ent.SaveChanges();
        }
        #endregion
    }
}
